import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_wan_android/arch/net/http_manager.dart';
import 'package:flutter_wan_android/arch/net/result_data.dart';
import 'package:flutter_wan_android/arch/ui/toast.dart';
import 'package:flutter_wan_android/common/api.dart';
import 'package:flutter_wan_android/model/three_data.dart';
import 'package:flutter_wan_android/res/colors.dart';
import 'package:flutter_wan_android/res/theme.dart';
import 'package:flutter_wan_android/routes/route_util.dart';
import 'package:flutter_wan_android/routes/routes.dart';

class SystemTixi extends StatefulWidget {
  @override
  _SystemTixiState createState() => _SystemTixiState();
}

class _SystemTixiState extends State<SystemTixi>
    with AutomaticKeepAliveClientMixin {
  List<ThreeData> _three = [];

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    _getData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(top: 8),
      child: _threeList(),
    );
  }

  _getData() async {
    ResultData result = await HttpManager.instance().get(Api.TIXI);

    if (result.success) {
      setState(() {
        _three = getThreeDataList(result.data);
      });
    } else {
      ToastUtil.show(result.msg);
    }
  }

  _threeList() {
    if (_three.isEmpty) return Container();

    return EasyRefresh(
      child: ListView.separated(
        itemBuilder: (BuildContext context, int index) {
          return _threeItem(_three[index]);
        },
        separatorBuilder: (BuildContext context, int index) {
          return Padding(padding: EdgeInsets.only(top: 1));
        },
        itemCount: _three.length,
      ),
    );
  }

  _threeItem(ThreeData item) {
    var child = item.children.map<Widget>((e) {
      return GestureDetector(
        onTap: () {
          var index = item.children.indexOf(e);
          RouteUtil.push(
            context,
            Routes.txList,
            params: {"data": item, "index": index},
          );
        },
        child: Container(
          padding: EdgeInsets.symmetric(vertical: 4, horizontal: 14),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(20)),
            color: wTheme.txItemBg,
          ),
          child: Text(
            e.name,
            style: TextStyle(fontSize: 13, color: wTheme.text1),
          ),
        ),
      );
    }).toList();

    return GestureDetector(
      onTap: () {
        RouteUtil.push(
          context,
          Routes.txList,
          params: {"data": item, "index": 0},
        );
      },
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 10),
        child: Card(
          elevation: 0,
          color: wTheme.listItemBg,
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(8))),
          child: Padding(
            padding: EdgeInsets.all(10),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  item.name,
                  style: TextStyle(fontSize: 15, color: WColor.blue1),
                ),
                Padding(padding: EdgeInsets.only(top: 12)),
                Wrap(
                  spacing: 10,
                  runSpacing: 10,
                  children: child,
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}
